package fr.umlv.projet.android.submit;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import fr.umlv.projet.android.andex.R;
import fr.umlv.projet.android.utils.StringUtils;
import fr.umlv.projet.android.zip.OutilsZip;

public class SubmitActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.submit);
		
		Button sendButton = (Button)findViewById(R.id.submitButton);
		Button backButton = (Button)findViewById(R.id.backButton);
		
		sendButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				
				String examName = getIntent().getStringExtra("exam");
				
				String str = Environment.getExternalStorageDirectory().getAbsolutePath();
				OutilsZip o = new OutilsZip();
				
				String test = null;
				InputStream is;
				try {
					o.zipFolder(str+"/Andex/"+examName, str+"/Andex/"+examName+".zip");
					is = new FileInputStream(str+"/Andex/"+examName+".zip");
					test = StringUtils.inputStreamToStringOfByte(is);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				// Call Submit Service
				Intent submitService = new Intent(getBaseContext(), SubmitService.class);
				submitService.putExtra("exam", examName);
				submitService.putExtra("data", test);

				startService(submitService);
				
				// Refresh screen
				Toast.makeText(getBaseContext(), "Submit OK", Toast.LENGTH_SHORT).show();
			}
		});
		
		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}
	
}
